﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

[RequireComponent(typeof(CharacterController))]
public class DoctorController : MonoBehaviour
{
    private CharacterController characterController;
    [Header("Movement Settings")]
    [SerializeField] private float walkSpeed = 5f;
    [SerializeField] private float runSpeed = 10f;
    [SerializeField] private float acceleration = 10f;
    [SerializeField] private float deceleration = 15f;

    private float currentSpeed;
    private Vector3 currentVelocity;
    private bool isRunning;


    [Header("Camera Settings")]
    [SerializeField] private float mouseSensitivity = 2.4f;
    [SerializeField] private float minVerticalAngle = -90f;
    [SerializeField] private float maxVerticalAngle = 90f;
    [SerializeField] private bool invertYAxis = false;//旋转方向是否反转。

    private float horizontalRotation; // 水平旋转角度
    private float verticalRotation;   // 垂直旋转角度
    private Transform cameraTrans;





    private float angleY;
    private float angleX;

    //private bool isGrounded;
    //public float jumpSpeed;
    //private CollisionFlags collisionFlags;
    //public int currentWeaponID;
    //private Dictionary<int,Weapon> weaponsDict;
    //private Transform weaponPlaceTrans;
    //private Dictionary<int, int> ammoInventory;//玩家的武器库（背包，当前玩家某个武器以及其剩余的子弹数量）
    //public int currentHP;
    //public int initHP;

    //public CameraShaker cameraShaker;
    //public bool dead;
    //public Transform deadPostionTrans;
    //public AudioClip jumpClip;
    //public AudioClip landClip;
    //private bool canPlayLandClip;
    //public AudioClip deadClip;
    //public AudioClip hurtClip;
    //public Weapon[] weapons;
    // Start is called before the first frame update
    void Start()
    {
        characterController = GetComponent<CharacterController>();
        angleY = transform.eulerAngles.y;
        cameraTrans = Camera.main.transform;
        angleX = cameraTrans.eulerAngles.x;
        //Cursor.visible = false;
        //Cursor.lockState = CursorLockMode.Locked;
        //runningSpeed = 10;
        //jumpSpeed = 0;
        //isGrounded = true;
        //currentWeaponID = -1;
        //weaponPlaceTrans = cameraTrans.Find("WeaponPlace");
        ////weapons = new List<Weapon>()
        ////{
        ////    weaponPlaceTrans.GetChild(0).GetComponent<Weapon>(),
        ////    weaponPlaceTrans.GetChild(1).GetComponent<Weapon>(),
        ////    weaponPlaceTrans.GetChild(2).GetComponent<Weapon>(),
        ////};
        //weaponsDict = new Dictionary<int, Weapon>();
        //ammoInventory = new Dictionary<int, int>();
        //currentHP = initHP;
        //UIManager.Instance.ShowOrHideWeaponUIView(false);
        //for (int i = 0; i < weapons.Length; i++)
        //{
        //    weapons[i].gameObject.SetActive(false);
        //    weapons[i].PickUp(this);
        //}
    }

    // Update is called once per frame
    void Update()
    {
        //if (dead)
        //{
        //    return;
        //}
        CalculateMovement();
        ApplyMovement();
        TurnAndLook();
        //Jump();
        //ChangeCurrentWeapon();
    }
    /// <summary>
    /// 移动
    /// </summary>
    private void CalculateMovement()
    {
        isRunning = Input.GetButton("Run");
        // 获取输入并规范化
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
        float targetSpeed = isRunning ? runSpeed : walkSpeed;
        //input.magnitude 计算向量长度的属性
        targetSpeed *= input.magnitude;
        //Mathf.Lerp用于在两个值之间进行线性插值
        currentSpeed = Mathf.Lerp(
            currentSpeed,
            targetSpeed,
            (targetSpeed > currentSpeed ? acceleration : deceleration) * Time.deltaTime
        );

        //transform.TransformDirection将向量从局部空间(Local Space)转换到世界空间(World Space)
        if (input.magnitude > 0.1f)
        {
            Vector3 moveDirection = transform.TransformDirection(
                new Vector3(input.x, 0, input.y));
            currentVelocity = moveDirection * currentSpeed;
        }
        else
        {
            currentVelocity = Vector3.Lerp(
                currentVelocity,
                Vector3.zero,
                deceleration * Time.deltaTime
            );
        }
    }

    private void ApplyMovement()
    {
        if (!characterController.isGrounded)
        {
            //Physics.gravity Unity中控制全局物理重力的向量
            currentVelocity.y += Physics.gravity.y * Time.deltaTime;
        }

        characterController.Move(currentVelocity * Time.deltaTime);
    }

    private void InitializeCamera()
    {
        cameraTrans = Camera.main.transform;
        //transform.eulerAngles.y 游戏对象绕世界坐标系Y轴旋转角度
        //cameraTrans.eulerAngles.x 摄像机绕其X轴的旋转角度
        horizontalRotation = transform.eulerAngles.y;
        verticalRotation = cameraTrans.eulerAngles.x;

        // 将角度标准化到-180到180范围
        if (verticalRotation > 180f) verticalRotation -= 360f;
    }

    private void TurnAndLook()
    {
        // 获取鼠标输入
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * (invertYAxis ? -1 : 1);

        // 水平旋转（角色整体）
        horizontalRotation += mouseX;
        // Quaternion.Euler将三维空间中的旋转角度（绕 X、Y、Z 轴的旋转量）转换为四元数表示形式，以便 Unity 的旋转系统（如 Transform.rotation）处理
        transform.rotation = Quaternion.Euler(0f, horizontalRotation, 0f);

        // 垂直旋转（仅摄像机）
        verticalRotation = Mathf.Clamp(verticalRotation - mouseY, minVerticalAngle, maxVerticalAngle);
        cameraTrans.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
    }

    /// <summary>
    /// 转向看
    /// </summary>
    //private void TurnAndLook()
    //{
    //    float turnAngle = Input.GetAxis("Mouse X") * mouseSensitivity;
    //    angleY = angleY + turnAngle;
    //    transform.eulerAngles = new Vector3(transform.eulerAngles.x, angleY, transform.eulerAngles.z);
    //    float lookAngle = -Input.GetAxis("Mouse Y") * mouseSensitivity;
    //    angleX = Mathf.Clamp(angleX + lookAngle, -90f, 90f);
    //    cameraTrans.eulerAngles = new Vector3(angleX, cameraTrans.eulerAngles.y, cameraTrans.eulerAngles.z);
    //}
    /// <summary>
    /// 跳跃
    /// </summary>
    //private void Jump()
    //{
    //    if (Input.GetButton("Jump")&&isGrounded)
    //    {
    //        isGrounded = false;
    //        jumpSpeed = 5;
    //        AudioSourceManager.Instance.PlaySound(jumpClip,0.8f,1.1f);
    //        canPlayLandClip = true;
    //    }
    //    if (!isGrounded)//跳起来了，不在地面上
    //    {
    //        jumpSpeed= jumpSpeed - 10 * Time.deltaTime;
    //        Vector3 jump = new Vector3(0,jumpSpeed*Time.deltaTime,0);
    //        collisionFlags= characterController.Move(jump);
    //        if (collisionFlags==CollisionFlags.Below)
    //        {
    //            jumpSpeed = 0;
    //            isGrounded = true;
    //        }
    //    }
    //    if (isGrounded&&collisionFlags==CollisionFlags.None)
    //    {
    //        if (canPlayLandClip)
    //        {
    //            canPlayLandClip = false;
    //            AudioSourceManager.Instance.PlaySound(landClip, 0.8f, 1.1f);
    //        }
    //        isGrounded = false;
    //    }
    //}
    /// <summary>
    /// 具体切换武器
    /// </summary>
    /// <param name="id"></param>
    //private void ChangeWeapon(int id)
    //{
    //    if (weaponsDict.Count == 0)
    //    {
    //        return; 
    //    }
    //    ////处理索引的上下边界
    //    //if (id >= weaponsDict.Count)
    //    //{
    //    //    id = 0;
    //    //}
    //    //else if (id <= -1)
    //    //{
    //    //    id = weaponsDict.Count - 1;
    //    //}
    //    if (id>weaponsDict.Keys.Max())
    //    {
    //        id = weaponsDict.Keys.Min();
    //    }
    //    else if(id<weaponsDict.Keys.Min())
    //    {
    //        id = weaponsDict.Keys.Max();
    //    }
    //    if (id == currentWeaponID)//只有一种武器时不切换,否则会出现颠簸颤抖的情况
    //    {
    //        return;
    //    }
    //    while (!weaponsDict.ContainsKey(id))
    //    {
    //        if (id>currentWeaponID)
    //        {
    //            id++;
    //        }
    //        else
    //        {
    //            id--;
    //        }
    //    }
    //    //隐藏上一把武器
    //    if (currentWeaponID!=-1)//排除第一次没有武器的情况
    //    {
    //        weaponsDict[currentWeaponID].PutAway();
    //    }
    //    //显示当前武器
    //    weaponsDict[id].Selected();
    //    currentWeaponID = id;
    //}
    /// <summary>
    /// 切换当前武器
    /// </summary>
    //public void ChangeCurrentWeapon(bool autoChange=false)
    //{
    //    if (autoChange)
    //    {
    //        //切换到最新拿到的一把
    //        //ChangeWeapon(weaponsDict.Count-1);
    //        ChangeWeapon(weaponsDict.Keys.Last());
    //    }
    //    else
    //    {
    //        if (Input.GetAxis("Mouse ScrollWheel") < 0)
    //        {
    //            ChangeWeapon(currentWeaponID + 1);
    //        }
    //        else if (Input.GetAxis("Mouse ScrollWheel") > 0)
    //        {
    //            ChangeWeapon(currentWeaponID - 1);
    //        }
    //        for (int i = 0; i < 10; i++)
    //        {
    //            if (Input.GetKeyDown(KeyCode.Alpha0 + i))
    //            {
    //                int num;
    //                if (i == 0)
    //                {
    //                    num = 10;
    //                }
    //                else
    //                {
    //                    num = i - 1;
    //                }
    //                if (weaponsDict.ContainsKey(num))
    //                {
    //                    ChangeWeapon(num);
    //                }
    //            }
    //        }

    //    }

    //}
    /// <summary>
    /// 拾取武器
    /// </summary>
    /// <param name="weaponName"></param>
    //public void PickUpWeapon(int weaponID)
    //{
    //    if (weaponsDict.ContainsKey(weaponID))
    //    {
    //        //补充弹药
    //        Weapon weapon = weaponsDict[weaponID];
    //        ammoInventory[weapon.GetID()] = weapon.GetInitAmount();
    //        weapon.clipContent = weapon.clipSize;
    //        if (currentWeaponID==weaponID)
    //        {
    //            UIManager.Instance.UpdateBulletNum(weapon.clipSize, weapon.GetInitAmount());
    //        }
    //    }
    //    else//当前这种名称的武器列表里没有
    //    {
    //        //GameObject weaponGo= Instantiate(Resources.Load<GameObject>("Prefabs/Weapons/"+weaponID.ToString()));
    //        //weaponGo.transform.SetParent(weaponPlaceTrans);
    //        //weaponGo.transform.localPosition = Vector3.zero;
    //        //weaponGo.transform.localRotation = Quaternion.identity;
    //        //weaponGo.gameObject.SetActive(false);
    //        //Weapon weapon = weaponGo.GetComponent<Weapon>();
    //        //weapon.PickUp(this);
    //        weapons[weaponID].clipContent = weapons[weaponID].clipSize;
    //        weaponsDict.Add(weaponID,weapons[weaponID]);
    //        ammoInventory.Add(weaponID, weapons[weaponID].GetInitAmount());
    //        ChangeWeapon(weaponID);
    //    }
    //}
    /// <summary>
    /// 获取某一种武器在武器库中子弹的剩余数量
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    //public int GetAmmoAmount(int id)
    //{
    //    int value = 0;
    //    ammoInventory.TryGetValue(id,out value);
    //    return value;
    //}
    /// <summary>
    /// 更新当前武器库中某一种武器装备剩余子弹的数量
    /// </summary>
    /// <param name="id"></param>
    /// <param name="value"></param>
    //public void UpdateAmmoAmount(int id,int value)
    //{
    //    if (ammoInventory.ContainsKey(id))
    //    {
    //        ammoInventory[id] += value;
    //    }
    //}
    //public void TakeDamage(int value)
    //{
    //    if (dead)
    //    {
    //        return;
    //    }      

    //    if (value<0)
    //    {
    //        if (currentHP<initHP)
    //        {
    //            currentHP -= value;
    //            if (currentHP>= initHP)
    //            {
    //                currentHP = initHP;
    //            }
    //        }
    //    }
    //    else
    //    {
    //        currentHP -= value;
    //    }

    //    if (currentHP <= 0)
    //    {
    //        dead = true;
    //        cameraTrans.localPosition = deadPostionTrans.localPosition;
    //        cameraTrans.eulerAngles = deadPostionTrans.eulerAngles;
    //        weaponPlaceTrans.gameObject.SetActive(false);
    //        currentHP = 0;
    //        UIManager.Instance.ShowDeadUI();
    //        AudioSourceManager.Instance.PlaySound(deadClip);
    //    }
    //    else
    //    {
    //        if (value>0)
    //        {
    //            AudioSourceManager.Instance.PlaySound(hurtClip);
    //            UIManager.Instance.ShowTakeDamageView();
    //            cameraShaker.SetShakeValue(0.2f, 0.5f);
    //        }

    //    }
    //    UIManager.Instance.UpdateHPValue(currentHP);
    //}
}
